﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace ceTerm
{    
    public partial class EtiketOkuma : XForm
    {
        TreeNode tnBarkod;
        TreeNode tnRFID;

        public EtiketOkuma()
        {
            InitializeComponent();

            this.WindowState = FormWindowState.Maximized;
            this.KeyPreview = true;

            //Terminal.RFID.SET_BuzzerOnOff(true);
            Terminal.SetActivatedForm(this);

            tnBarkod = tvTop.Nodes[0];
            tnRFID = tvTop.Nodes[1];
            tnRFID.ExpandAll();

            tmrRefresher.Tick+=new EventHandler(tmrRefresher_Tick);
            tmrRefresher.Enabled = true;
            tvTop.Focus();

            tvTop.AfterSelect += new TreeViewEventHandler(tvTop_AfterSelect);
        }

        void tvTop_AfterSelect(object sender, TreeViewEventArgs e)
        {
            UpdateButtonStatus();
        }


        private void UpdateButtonStatus()
        {
            lDetay.Enabled = false;
            lDetay.Tag = string.Empty;

            foreach (TreeNode tnBarkod in tvTop.Nodes)
                foreach (TreeNode tn in tnBarkod.Nodes)
                    //foreach (TreeNode tn in tnKumas.Nodes)
                    if (tn.IsSelected)
                    {
                        lDetay.Enabled = true;
                        lDetay.Tag = tn.Text;
                        return;
                    }
        }

        List<int> RFID = new List<int>();
        private void tmrRefresher_Tick(object sender, EventArgs e)
        {
            for (int i = tnRFID.Nodes.Count - 1; i >= 0; i--)
                if (tnRFID.Nodes[i].Tag is DateTime)
                    if (((DateTime)tnRFID.Nodes[i].Tag).AddSeconds(1) < DateTime.Now)
                    {
                        lRFID.Remove(intParse(tnRFID.Nodes[i].Text));
                        tnRFID.Nodes[i].Remove();
                    }

            tnRFID.Text = "RFID" + (tnRFID.Nodes.Count > 0 ? ": " + tnRFID.Nodes.Count.ToString() : string.Empty);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F18: // AT570, AT870 Gun Trigger 
                case Keys.F19: // AT570, AT870 Gun Trigger                    
                    Terminal.StartBarcode();
                    break;

                default:
                    break;
            }
            base.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F18: // AT570, AT870 Gun Trigger 
                case Keys.F19: // AT570, AT870 Gun Trigger                     
                    Terminal.StopBarcode();
                    break;

                default:
                    break;
            }
            base.OnKeyUp(e);
        }

        List<int> lRFID = new List<int>();
        //IUHFHost Members
        public override void GetAccessEPC(string EPC)
        {
            //string[] temp = EPC.Split(",".ToCharArray());

            //int EPCint = int.Parse(temp[0].Substring(4, temp[0].Length - 4));
            //if (EPCint < 1)
            //    return;

            //string RSSI = temp[1].Split("=".ToCharArray())[1];
            //double dRSSI = double.Parse(RSSI);
            //bool hit = false;

            int EPCint;
            double dRSSI;
            this.SuspendLayout();

            resolveEPC(EPC, out EPCint, out dRSSI);

            if (lRFID.Contains(EPCint))
                //Nodes.Find("some node name", true);  olmadığı için iteration dan başka çare yok!
                foreach (TreeNode tnTop in tnRFID.Nodes)
                {
                    if (iText(tnTop) == EPCint)
                    {
                        //OkunanToplaraEkle(EPCint, dRSSI);

                        double doublePower = ((100 + dRSSI) - 35.9) * 9;
                        int power = (int)Math.Floor(doublePower);
                        if (power > 255) power = 255;
                        if (power < 0) power = 0;

                        tnTop.BackColor = Color.FromArgb(255 - power, 255, 255 - power);
                        tnTop.Tag = DateTime.Now;    // güncelleniyor
                        break;
                    }
                }
            else
            {
                lRFID.Add(EPCint);
                TreeNode node = tnRFID.Nodes.Add(EPCint.ToString());
                node.Tag = DateTime.Now;    // EPCint.ToString();
                node.BackColor = Color.White;
                //ResetIdleTimer();
            }

            tnRFID.ExpandAll();
            tnRFID.Text = "RFID" + (tnRFID.Nodes.Count > 0 ? ": " + tnRFID.Nodes.Count.ToString() : string.Empty);
            this.ResumeLayout();
        }

        public override void lDetay_Click(object sender, EventArgs e)
        {
            //    Browse("TopDetay.aspx?TopId=" + tvTop.SelectedNode.Text, true, "Top Detayı");
            Browse("TopDetay.aspx?TopId=" + lDetay.Tag.ToString(), true, "Top Detayı");
        }

        private void resolveEPC(string EPC, out int EPCi, out double RSSI)
        {
            if (EPC.Length > 20 && EPC[0] == '3')
                EPC = EPC.Remove(0, 1);

            if (EPC.Contains(","))
            {
                string[] temp = EPC.Split(",".ToCharArray());

                EPCi = intParse(temp[0]);
                RSSI = Double.Parse(temp[1].Replace("RSSI=", ""));
                //(int)((80+Double.Parse(temp[1].Replace("RSSI=", "")))*2);    //30-80 arası
            }
            else
            {
                EPCi = intParse(EPC);
                RSSI = 0;
            }
        }


        //NumericUpDown power = new NumericUpDown();
        //TrackBar power = new TrackBar();

        //IScan Members
        public override void OnScaned(string BarcodeValue, string TypeName, string TypeID)
        {
            Terminal.StopBarcode();
            CleanBarcode(ref BarcodeValue, TypeName);

            int barcode = intParse(BarcodeValue);
            //if (barcode < 1)
            //    return;

            //if (BarkodTipi(barcode) == barkodTipleri.Top)
            {
                Terminal.PlaySuccess();
                //tnBarkod.Nodes.Clear();
                tnBarkod.Nodes.Add(BarcodeValue);
                tnBarkod.ExpandAll();
            }
        }


    }
}